import { Component, OnInit } from '@angular/core';

import { BladeContainerBase } from "../../../../shared/blade/blade-container-base";
import { Setofkey } from "../../../../entity/setofkey.model";
import { SetofkeyService } from "../../../../service/setofkey.service";
import { Observable } from "rxjs/Observable";
import { Command } from "../../../../shared/toolbar.service";
import { Blade } from "../../../../shared/blade/blade.model";
import { BladeData } from "../../../../shared/blade/blade-data";
import { DialogResult } from "../../../../shared/dialog.service";

import { UserAlarmconfig } from "../../../../entity/user_alarmconfig.model";
import { AlarmBasicConfigEditComponent } from "../alarm-basic-config-edit/alarm-basic-config-edit.component"
import { UserAlarmconfigService } from "../../../../service/user_alarmconfig.service";

import { User } from "../../../../entity/user.model";

@Component({
	selector: 'app-alarm-basic-config-list',
	templateUrl: './alarm-basic-config-list.component.html',
	styleUrls: ['./alarm-basic-config-list.component.css']
})
export class AlarmBasicConfigListComponent extends BladeContainerBase<UserAlarmconfig>  {

	public alarmconfigData: UserAlarmconfig[] = [];
	public original: UserAlarmconfig[] = [];

	constructor(
		private userAlarmconfigService: UserAlarmconfigService,
	) {
		super()
	}

	ngOnInit(): void {
		this.commands = [
			new Command("edit", "配置", "fa fa-edit", "setofkey:update", blade => this.exeEdit(blade), blade => this.canExeEdit(blade)),
		];
		super.ngOnInit();
	}

	initData(): any | Observable<any> | Promise<any> {
		this.userAlarmconfigService.getUserAlarmconfigsByUserId(this.parentEntity.userId)
			.then(res => {
				if(!res) return false;
				this.original = [...res];
				this.alarmconfigData = [...this.original];
				const index = this.alarmconfigData.findIndex(o => o.alarmConfigName === 'SoundVolume');
				this.alarmconfigData.splice(index, 1);
				this.alarmconfigData.forEach(o => {
					if (o.alarmConfigValue === 'false') {
						o.alarmConfigValue = false;
						o.configValue = '×';
					} else if (o.alarmConfigValue === 'true') {
						o.alarmConfigValue = true;
						o.configValue = '✔';
					} else {
						o.alarmConfigValue = Number(o.alarmConfigValue);
						o.configValue = Number(o.alarmConfigValue);
					}
				});
			})
			.catch(error => this.bladeData.error = error)
			.then(res => this.bladeData.isLoading = false);

	}

	exeEdit(blend: Blade): void {
		if (this.currentEntity != undefined) {
			let data: BladeData = new BladeData();
			data.id = "alarmConfigEdit";
			data.title = "设置";
			let editalarmConfigBlade = this.bladeService.createBlade(AlarmBasicConfigEditComponent, data, false, this.currentEntity);
			this.currentBlade.bladeService.showBlade(editalarmConfigBlade, this.currentBlade);
		}
	}

	canExeEdit(blend?: Blade): boolean {
		if (this.currentEntity != null) {
			return true;
		}
	}

	onSeletedChange(selectedAlarm: UserAlarmconfig) {
		if (this.currentBlade.childrenCanDeactivate()) {
			this.currentEntity = selectedAlarm;
			this.currentEntity.userId = this.parentEntity.userId;
		}
	}

}



// WEBPACK FOOTER //
// ./src/app/module/basic/alarm-basic-config/alarm-basic-config-list/alarm-basic-config-list.component.ts